#database.py - Performs read/write operations on a file containing event entries.
__author__ = "Lewis Marhin"

import unittest

# tsv (tab seperated values) files can be opened by spreadsheet processors
# just like csv files.
fileN = "cal.tsv"
delim = "\t"

global fileN
global delim
    
# If multiple strings are passed they will be written individually with
# correct formatting.
def add(*aStr1): 
    txtFile = open(fileN, "a")
    for everyOne in aStr1:
        txtFile.write(everyOne)
        txtFile.write(delim)
    txtFile.write("\n")
    txtFile.close()

# For getting all the entries.
def get():
    txtFile = open(fileN, "r")
    tmp = txtFile.read()
    txtFile.close()
    return tmp

# For getting a specific entry.
def get(theID):
    tmp = ""
    txtFile = open(fileN, "r")
    for line in txtFile:
        if checkID(theID, line) == True:
            tmp = line
            break
    txtFile.close()
    return tmp

# Checks the event ID against a string type event.
def checkID(theID, theString):
    tmpID = ""
    index = 0
    
    for letters in theString:
        if theString[index] == "\t":
            break
        else:
            tmpID += theString[index]
        index += 1

    if tmpID == theID:
        return True
    else:
        return False

# For editing an existing entry
def edit(theID, theEvent):
    erase(theID)
    add(theEvent)

# For erasing a specific entry.
# Note - May seem inefficient but reduces memory overhead and
# the chance of file corruption.
def erase(theID):
    tmp = ""
    txtFile = open(fileN, "r")
    tmpFile = open("bkup_" + fileN, 'w')
    for line in txtFile:
        if checkID(theID, line) == False:
            tmpFile.write(line)
    tmpFile.close()
    txtFile.close()
    txtFile = open(fileN, "w")
    tmpFile = open("bkup_" + fileN, 'r')
    txtFile.write(tmpFile.read())
    tmpFile.close()
    txtFile.close()

# Mostly for testing purposes, erases the contents of the text file.
def eraseAll():
    txtFile = open(fileN, "w")
    txtFile.close()

class Tester(unittest.TestCase):
    # Tests to check if ID checking works.
    def testCheckIDTrue(self):
        self.assert_(True == checkID("568", "568\tTutorial\t564\t"))

    def testCheckIDFalse(self):
        self.assert_(False == checkID("551", "568\tTutorial\t564\t"))

    # Tests file write without preformatted text.
    def testReadWrite1(self):
        add("30", "Lecture", "12:00", "12:50", "XK2455", "Bob", "Z3.343", "Repeat:Weekly")
        add("31", "Tutorial", "1:00", "1:50", "XK3578", "Bob", "Z3.343", "Repeat:Weekly")
        add("32", "Lecture", "2:00", "2:50", "XK2311", "Bob", "Z3.343", "Repeat:Weekly")
        add("33", "Practical", "3:00", "3:50", "ZR4314", "Bob", "Z3.343", "Repeat:Weekly")
        self.assert_(get("32") == "32" + delim + "Lecture" + delim + "2:00" + delim + "2:50" + delim + "XK2311" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly" + delim + "\n")
        eraseAll()

    # Tests read failure.
    def testReadFailure(self):
        add("30", "Lecture", "12:00", "12:50", "XK2455", "Bob", "Z3.343", "Repeat:Weekly")
        add("31", "Tutorial", "1:00", "1:50", "XK3578", "Bob", "Z3.343", "Repeat:Weekly")
        add("32", "Lecture", "2:00", "2:50", "XK2311", "Bob", "Z3.343", "Repeat:Weekly")
        add("33", "Practical", "3:00", "3:50", "ZR4314", "Bob", "Z3.343", "Repeat:Weekly")
        self.assert_(get("34") == "")
        eraseAll()

    # Tests file write with preformatted text.
    def testReadWrite2(self):
        txt = "30" + delim + "Lecture" + delim + "12:00" + delim + "12:50" + delim + "XK2455" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly"
        txt2 = "31" + delim + "Tutorial" + delim + "1:00" + delim + "1:50" + delim + "XK3578" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly"
        txt3 = "32" + delim + "Lecture" + delim + "2:00" + delim + "2:50" + delim + "XK2311" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly"
        txt4 = "33" + delim + "Practical" + delim + "3:00" + delim + "3:50" + delim + "ZR4314" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly"
        add(txt)
        add(txt2)
        add(txt3)
        add(txt4)
        self.assert_(get("31") == txt2 + "\t\n")
        eraseAll()

    # Test if editting is working correctly
    def testEdit(self):
        txt = "30" + delim + "Lecture" + delim + "12:00" + delim + "12:50" + delim + "XK2455" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly"
        txt2 = "30" + delim + "Practical" + delim + "12:00" + delim + "12:50" + delim + "XK2455" + delim + "Bob" + delim + "Z3.343" + delim + "Repeat:Weekly"
        add(txt)
        edit("30", txt2)
        self.assert_(get("30") == txt2 + "\t\n")

if __name__ == "__main__":
    unittest.main()
